package com.example.project.Controller;

import com.example.project.Service.PictureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class PictureController {

    @Autowired
    private PictureService pictureService;

    @PostMapping("/UpPicture")
    public Map<String, Object> UpPicture(@RequestParam("picture") MultipartFile files) throws IOException {
        Map<String, Object> map = new HashMap<>();
        try{
            String url = pictureService.upLoadPicture(files);
            if(url.equals("-2")){
                map.put("errno", "14001");
                map.put("message", "图片格式不正确！");
                return map;
            }
            if(url.equals("-1")){
                map.put("errno", "14002");
                map.put("message", "图片上传失败！");
                return map;
            }
            map.put("errno", "1");
            map.put("message", "图片上传成功！");
            map.put("result", url);
        } catch (Exception e){
            map.put("errno", "14002");
            map.put("message", "图片上传失败！");
        }
        return map;
    }
}
